Skip to content

DEV-1198 Fix the ShimmerDriverPC connect test hang on Linux CI - #325

Merged
JongChern merged 2 commits into
masterfrom
DEV-1198_connect_test_hang
Oct 15, 2026
Merged

JongChern merged 2 commits into
masterfrom
DEV-1198_connect_test_hang

Conversation

@JongChern

@JongChern JongChern commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the hang that has kept gradle.yml red on master and on every PR since #321 merged on 2026-10-12. The failing test was API_0000X_ByteCommunicationShimmer3lns0_16_11_w_sd_bt_streaming.test001_testConnectandDisconnect ("Device did not finish initialising within 30000 ms (state: Connecting)").

Cause: a lock-order deadlock between the IOThread and the read-status timer

ShimmerBluetooth's instruction list is guarded by two locks: the device's monitor and the list's own. Every path takes them device first, list second (getListofInstructions(), writeInstruction(), …), except getInstruction() on the IOThread. It holds the list's lock and then calls the synchronized getListofInstructions().

initializeShimmer3or3R() unlocks the instruction stack and then starts the read-status timer with no delay, just as the IOThread picks up the first post-connect instruction. The timer's readStatusTask calls getListofInstructions(), so it takes the two locks the other way round. When they meet, both threads block for good. The connect never finishes, and disconnect() cannot stop the IOThread ("IOThread did not terminate within join timeout").

The thread dump this PR adds shows the cycle on Linux CI (run 38681789546, in lns0_16_11_w_sd_bt_streaming), and on a local Windows run in API_0000X_ByteCommunicationShimmer3:

IOThread           BLOCKED on ShimmerPC@…  owned by Shimmer__TimerReadStatus   (holds ArrayList, in getInstruction)
TimerReadStatus    BLOCKED on ArrayList@…  owned by IOThread                   (holds ShimmerPC, in getListofInstructions)

#321 did not cause it. Before #321 each test slept 3-5 s after connecting. Now connects follow one another as soon as each device is initialised, which made the race land on CI every time.

Changes (test code only, the driver is unchanged)

  • Diagnostics: ByteCommunicationTestUtils.connectAndWaitUntilInitialised prints every thread's full stack, the monitors it holds and any JVM-detected deadlock before failing on a timeout.
  • Fix in the tests: the simulator test classes build their device through ByteCommunicationTestUtils.newShimmerPCWithoutStatusTimer(), a ShimmerPC whose startTimerReadStatus() does nothing. That timer is the only other thread touching the instruction list while the IOThread connects: the connect sequence queues its reads while the stack is locked, and the battery timer first fires after 60 s. The tests lose only the 5 s status poll. The connect sequence still reads the status once.

No timeouts were raised and no tests were reordered.

Known limitation

The lock-order inversion is still in the driver. A real LogAndStream device can, rarely, stick at Connecting when the timer's first run meets the IOThread taking an instruction. Changing the driver's IO path was ruled out for this PR.

Verification

  • Full ShimmerDriverPC suite locally (Windows, JDK 11): 3 runs of 3 green, 231 tests across ShimmerDriverPC, ShimmerDriver and ShimmerBluetoothManager. The race rarely lands on Windows, so this shows the tests still pass without the 5 s status poll rather than that the hang is gone.
  • Linux CI: this PR's runs.
  • The three classes excluded since Feb 2026 (lns0_16_11_w_sd_only, w_sync_master, w_sync_slave) stay excluded here. Re-enabling them is a separate PR, so a failure in them cannot turn master red.

Other APIs

None need this: the change is confined to ShimmerDriverPC's tests.

🤖 Generated with Claude Code

The lns0_16_11_w_sd_bt_streaming connect hangs on Linux CI and not on
Windows, so the CI log is the only evidence of where the IO thread is
stuck. Print every thread's full stack, the monitors it holds, the lock
it waits on and any JVM-detected deadlock before failing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JongChern
JongChern marked this pull request as ready for review October 15, 2026 05:34
The connect sequence unlocks the instruction stack and then starts the
read-status timer with no delay, so the timer's first readStatusTask
(device lock, then instruction-list lock) runs just as the IOThread takes
the first post-connect instruction in getInstruction() (list lock, then
device lock). When they meet, both block for good and the connect never
finishes. The thread dump from the previous commit shows that cycle on
Linux CI in lns0_16_11_w_sd_bt_streaming, and locally in
API_0000X_ByteCommunicationShimmer3.

The driver is deliberately left unchanged. The simulator tests now build
their ShimmerPC through ByteCommunicationTestUtils.newShimmerPCWithoutStatusTimer(),
which overrides startTimerReadStatus() to do nothing, so nothing else
touches the instruction list while the IOThread connects. The tests lose
only the 5 s status poll; the connect sequence still reads the status
once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JongChern
JongChern force-pushed the DEV-1198_connect_test_hang branch from 3f1060b to ffb9647 Compare October 15, 2026 08:49
@JongChern
JongChern merged commit cfbb8f1 into master Oct 15, 2026
3 checks passed
@JongChern
JongChern deleted the DEV-1198_connect_test_hang branch October 15, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants